<?php
// $Id: matchdart.admin.inc 182 2010-03-13 17:49:35Z mwi.nor $

/**
 * @file
 * internal functions for the matchdart module.
 */

/**
 * Returns matchdart_series for select boxes
 *
 * @param $option = FALSE Show 'all' or 'none' option value
 * @return array An option array containing the matchdart_series with fields:
 * @return value: nid
 * @return label: title
 * @see matchdart_form()
 */
function _matchdart_get_series_select($option = FALSE) {
  $array = array();
  $result = db_query(db_rewrite_sql("SELECT nid, title FROM {node} WHERE type = 'matchdart_series' and status = '1' ORDER BY title"));

  switch ($option) {
    case 'all':
      $array[0] = t('- all -');
      break;
    case 'none':
      $array[0] = t('- none -');
      break;
  };

  while ($row = db_fetch_object($result)) {
    $array[$row->nid] = $row->title;
  }

  return $array;
}

/**
 * Returns matchdart_types for select boxes
 *
 * @param $type = NULL matchdart_type content type to display
 * @param $option = FALSE Show 'all' or 'none' option value
 * @return $events An option array containing the matchdart_events with fields:
 * @return value: nid
 * @return label: title
 * @see matchdart_form()
 */
function _matchdart_get_nodeselect($type = NULL, $option = FALSE, $parent_id = 0) {
  $array = array();
  
  if ($type == 'matchdart_event' AND $parent_id <> 0) {
    $result = db_query(db_rewrite_sql("SELECT n.nid, n.title FROM {node} n INNER JOIN {content_type_matchdart_event} mde ON n.nid = mde.nid WHERE n.type = 'matchdart_event' and n.status = '1' and mde.field_mde_series_nid = %d ORDER BY title"), $parent_id);
  } else {
    $result = db_query(db_rewrite_sql("SELECT nid, title FROM {node} WHERE type = '%s' and status = '1' ORDER BY title"), $type);
  }

  switch ($option) {
    case 'all':
      $array[0] = t('- all -');
      break;
    case 'none':
      $array[0] = t('- none -');
      break;
  };

  while ($row = db_fetch_object($result)) {
    $array[$row->nid] = $row->title;
  }

  return $array;
}

/**
 * Returns the drupal users for select boxes
 *
 * @param $option = FALSE Show 'all' or 'none' option value
 * @return $array An option array containing the matchdart_users with fields:
 * @return value: uid
 * @return label: name
 * @see matchdart_form()
 */
function _matchdart_get_userselect($option = FALSE) {
  $array = array();
  $result = db_query(db_rewrite_sql("SELECT u.name, u.uid FROM {users} u ORDER BY u.name ASC"));

  switch ($option) {
    case 'all':
      $array[0] = t('- all -');
      break;
    case 'none':
      $array[0] = t('- none -');
      break;
  };

  while ($row = db_fetch_object($result)) {
    $array[$row->uid] = $row->name;
  }
  return $array;
}

/**
 * Returns the matchdart_event
 *
 * @package matchdart
 * @author mwi.nor, <mailto://info@matchdart.de>
 * @param $nid The {node} type matchdart_event nid
 * @return $array An array containing the matchdart_event with fields:
 * @return title, field_mde_points_draw_value, field_mde_points_win_value, field_mde_points_lose_value
 * @see matchdart_block(), matchdart_match.module
 */
function _matchdart_get_event($nid) {
  $result = db_query(db_rewrite_sql("SELECT n.nid, ".
                                    "n.title, ".
                                    "mde.field_mde_date_value, ".
                                    "mde.field_mde_series_nid, ".
                                    "mde.field_mde_points_draw_value, ".
                                    "mde.field_mde_points_win_value, ".
                                    "mde.field_mde_points_lose_value ".
                                    "FROM {node} n ".
                                    "JOIN {content_type_matchdart_event} mde ON n.nid = mde.nid ".
                                    "WHERE n.nid = %d"), $nid);
  $array = db_fetch_array($result);
  return($array);
}

/**
 * Returns the matchdart_series
 *
 * @package matchdart
 * @param $nid The {node} type matchdart_series nid
 * @return $array An array containing the matchdart_series with fields:
 * @return nid, title
 * @see matchdart_block(), matchdart_match.module
 */
function _matchdart_get_series($nid) {
  $result = db_query(db_rewrite_sql("SELECT n.nid, ".
                                    "n.title ".
                                    "FROM {node} n ".
                                    "WHERE n.nid = %d"), $nid);
  $array = db_fetch_array($result);
  return($array);
}

/**
 * Returns the matchdart_player profiles with matches in the actual event
 *
 * @package matchdart
 * @param $event_nid The event nid (field_mdm_match_event_nid) for {content_type_matchdart_match}
 * @return $array An array containing the matchdart_players with fields:
 * @return nid, title
 * @see matchdart_block()
 */
function _matchdart_get_event_players($event_nid) {
  $event_select = ($event_nid == 0) ? "" : "AND mdm.field_mdm_match_event_nid = ".$event_nid;
  $result = db_query(db_rewrite_sql("SELECT DISTINCT ".
                                    "n.nid, ".
                                    "n.title ".
                                    "FROM {content_type_matchdart_match} mdm, {node} n ".
                                    "WHERE (n.nid = mdm.field_mdm_home_player_nid OR n.nid = mdm.field_mdm_away_player_nid) ".
                                    "%s"), $event_select);

  while ($row = db_fetch_array($result)) {
    $array[] = $row;
  }
  
  return($array);
}

/**
 * Returns the matchdart_match results of a single player in the selected series / event
 *
 * @package matchdart
 * @param $player_nid, $event_nid The player nid (field_mdm_home_player_nid), event nid (field_mdm_match_event_nid) for {content_type_matchdart_match}
 * @return $array An array containing the matchdart_match results
 * @see matchdart_block()
 */
function _matchdart_get_results($player_nid, $series_nid, $event_nid) {
  $series_select = ($series_nid == 0 AND $event_nid != 0) ? "" : "AND field_mdm_match_series_nid = ".$series_nid;
  $event_select = ($event_nid == 0) ? "" : "AND field_mdm_match_event_nid = ".$event_nid;
  $sql = "SELECT ".
    "COUNT(IF(field_mdm_home_player_nid = ".$player_nid.",1,NULL)) as home_matches, ".
    "COUNT(IF((field_mdm_home_player_nid = ".$player_nid." AND field_mdm_match_result_value = 1),1,NULL)) as home_wins, ". 
    "COUNT(IF((field_mdm_home_player_nid = ".$player_nid." AND field_mdm_match_result_value = 2),1,NULL)) as home_loses, ".
    "COUNT(IF((field_mdm_home_player_nid = ".$player_nid." AND field_mdm_match_result_value = 0),1,NULL)) as home_draws, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_points_value + field_mdm_home_bonus_value,0)) as home_points, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_legs_value,0)) as home_leg_wins, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_away_legs_value,0)) as home_leg_loses, ".
    "MAX(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_highfinish_value,0)) as home_highfinish, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_180_value,0)) as home_180s, ".
    "COUNT(IF(field_mdm_away_player_nid = ".$player_nid.",1,NULL)) as away_matches, ".
    "COUNT(IF((field_mdm_away_player_nid = ".$player_nid." AND field_mdm_match_result_value = 2),1,NULL)) as away_wins, ". 
    "COUNT(IF((field_mdm_away_player_nid = ".$player_nid." AND field_mdm_match_result_value = 1),1,NULL)) as away_loses, ".
    "COUNT(IF((field_mdm_away_player_nid = ".$player_nid." AND field_mdm_match_result_value = 0),1,NULL)) as away_draws, ".
    "SUM(IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_points_value + field_mdm_away_bonus_value,0)) as away_points, ".
    "SUM(IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_legs_value,0)) as away_leg_wins, ".
    "SUM(IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_home_legs_value,0)) as away_leg_loses, ".
    "MAX(IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_highfinish_value,0)) as away_highfinish, ".
    "SUM(IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_180_value,0)) as away_180s, ".
    "COUNT(nid) as total_matches, ".
    "COUNT(IF((field_mdm_home_player_nid = ".$player_nid." AND field_mdm_match_result_value = 1) OR (field_mdm_away_player_nid = ".$player_nid." AND field_mdm_match_result_value = 2),1,NULL)) as total_wins, ". 
    "COUNT(IF((field_mdm_home_player_nid = ".$player_nid." AND field_mdm_match_result_value = 2) OR (field_mdm_away_player_nid = ".$player_nid." AND field_mdm_match_result_value = 1),1,NULL)) as total_loses, ".
    "COUNT(IF((field_mdm_home_player_nid = ".$player_nid." AND field_mdm_match_result_value = 0) OR (field_mdm_away_player_nid = ".$player_nid." AND field_mdm_match_result_value = 0),1,NULL)) as total_draws, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_points_value + field_mdm_home_bonus_value,IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_points_value + field_mdm_away_bonus_value,0))) as total_points, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_legs_value,IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_legs_value,0))) as total_leg_wins, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_away_legs_value,IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_home_legs_value,0))) as total_leg_loses, ".
    "IF(MAX(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_highfinish_value,0)) > MAX(IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_highfinish_value,0)), MAX(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_highfinish_value,0)), MAX(IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_highfinish_value,0))) as total_highfinish, ".
    "SUM(IF(field_mdm_home_player_nid = ".$player_nid.",field_mdm_home_180_value,IF(field_mdm_away_player_nid = ".$player_nid.",field_mdm_away_180_value,0))) as total_180s ".
    "FROM {content_type_matchdart_match} ".
    "WHERE (field_mdm_home_player_nid = ".$player_nid." OR field_mdm_away_player_nid = ".$player_nid.") ".
    "%s %s;";

  $result = db_query(db_rewrite_sql($sql), $season_select, $event_select);
  $array = db_fetch_array($result);
  return($array);
}

/**
 * Returns the matchdart_matches in the selected series / event
 *
 * @package matchdart
 * @param $series_nid, $event_nid
 * @return $array An array containing the matchdart_matches
 * @see matchdart_matches()
 */
function _matchdart_get_matches($series_nid, $event_nid) {
  $series_select = ($series_nid == 0 AND $event_nid != 0) ? "" : "WHERE field_mdm_match_series_nid = ".$series_nid;
  $event_select = ($event_nid == 0) ? "" : "WHERE field_mdm_match_event_nid = ".$event_nid;
  $sql = "SELECT * ".
    "FROM {content_type_matchdart_match} ".
    " %s %s;";

  $result = db_query(db_rewrite_sql($sql), $season_select, $event_select);
  $row = db_fetch_array($result);

  while ($row = db_fetch_array($result)) {
    $array[] = $row;
  }
  
  return($array);
}

/**
 * Returns the matchdart_player profile
 *
 * @package matchdart
 * @param $nid The nid for {node} type matchdart_player
 * @return $array An array containing the matchdart_player with fields:
 * @return title
 * @see matchdart_block(), matchdart_match.module
 */
function _matchdart_get_player($nid) {
  $result = db_query(db_rewrite_sql("SELECT title
  FROM {node}
   WHERE nid = %d"), $nid);
  $array = db_fetch_array($result);
  return($array);
}

/**
 * Returns a unix timestamp value for date select fields
 *
 * @package matchdart
 * @return $timestamp A unix timestamp
 *
 */
function _matchdart_get_timestamp($date = NULL, $date_api = FALSE) {
  global $user;
  dsm($user);
  if ($date_api) {
    $timestamp = mktime($date);
  } else {
    $timestamp = mktime(0,0,0,$date['month'],$date['day'],$date['year']);
  }
  
  if (variable_get('configurable_timezones', 1) && $user->uid && strlen($user->timezone)) {
    $timezone = $user->timezone;
  }
  else {
    $timezone = variable_get('date_default_timezone', 0);
  }
  $timestamp += $timezone;

  return $timestamp;
}

/**
 * Returns the date for forms
 *
 * @package matchdart
 * @return $date An array with the keys: 'year', month', and 'day'
 *
 */
function _matchdart_get_date($timestamp = 0) {
  $date = array(
        'year' => format_date($timestamp, 'custom', 'Y'),
        'month' => format_date($timestamp, 'custom', 'n'),
        'day' => format_date($timestamp, 'custom', 'j')
  );
  return $date;
}

/**
 * matchdart_sort_points
 *
 * Returns Array() sorted by 'points'
 *
 * @package matchdart
 * @author mawi, <mailto://info@matchdart.de>
 * @param $a, $b array rows to sort
 */
function _matchdart_sort_points($a, $b)
{
  if ($a['total_points'] == $b['total_points'])
  {
    return 0;
  } else
  {
    return ($a['total_points'] > $b['total_points']) ? -1 : 1;
  }
}
